//
//  DataHandle.m
//  旅游咨询
//
//  Created by 徐斌 on 15/4/20.
//  Copyright (c) 2015年 xubin. All rights reserved.
//

#import "DataHandle.h"

@implementation DataHandle

#define MAIN     ([[UIScreen mainScreen]bounds])

+ (instancetype)sharDataBaseHandle
{
    static DataHandle *dataHandle;
    if (dataHandle == nil) {
        dataHandle = [[DataHandle alloc]init];
        
    }
    
    return dataHandle;
}

static sqlite3 *db = nil;

- (void)openDB
{
    if (db != nil) {
        NSLog(@"数据库已经打开了");
        return;
    }
    
//    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *documentPath = [arr lastObject];
//    NSString *sqlsitePath = [documentPath stringByAppendingPathComponent:@"movie.sqlite"];
    
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSString *sqlitePath = [documentPath stringByAppendingPathComponent:@"travelHomePage.sqlite"];
    NSLog(@"%@", sqlitePath);
    
    BOOL result = sqlite3_open(sqlitePath.UTF8String, &db);
    if (result == SQLITE_OK) {
        NSLog(@"打开数据库");
        
    }
    else
    {
        NSLog(@"打开数据库失败");
    }
}

- (void)creatDB
{
    NSString *creatSql = @"CREATE TABLE IF NOT EXISTS travelHomePage(name TEXT, popular_place_str TEXT, cover_image TEXT, avatar_s TEXT, userName TEXT, title TEXT, detailid TEXT)";
    BOOL result = sqlite3_exec(db, creatSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
        
    }
    else
    {
        NSLog(@"创建表失败");
    }
}

- (void)insertTravelNotes:(TravelHomeModel *)notesModel{
    NSString *inserSql = [NSString stringWithFormat:@"INSERT INTO travelHomePage(name, popular_place_str, cover_image, avatar_s, userName, title, detailid) VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@')", notesModel.name, notesModel.popular_place_str, notesModel.cover_image, notesModel.avatar_s, notesModel.userName, notesModel.title, notesModel.detailid];
    int result = sqlite3_exec(db, inserSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }
    else
    {
        NSLog(@"插入失败");
    }
    
}

- (NSMutableArray *)selectAllStudent
{
    NSString *selectSql = @"SELECT * FROM travelHomePage";
    sqlite3_stmt *stmt = nil;
    
    BOOL result = sqlite3_prepare_v2(db, selectSql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"db查询准备成功");
        NSMutableArray *array = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
           
            const unsigned char *name = sqlite3_column_text(stmt, 0);
            const unsigned char *popular_place_str = sqlite3_column_text(stmt, 1);
            const unsigned char *cover_image = sqlite3_column_text(stmt, 2);
            const unsigned char *avatar_s = sqlite3_column_text(stmt, 3);
            const unsigned char *userName = sqlite3_column_text(stmt, 4);
            const unsigned char *title = sqlite3_column_text(stmt, 5);
            const unsigned char *detailid = sqlite3_column_text(stmt, 6);
 
           
            NSString *nameStr = [NSString stringWithUTF8String:(const char *)name];
            NSString *popular_place_strStr = [NSString stringWithUTF8String:(const char *)popular_place_str];
            NSString *cover_imageStr = [NSString stringWithUTF8String:(const char *)cover_image];
            NSString *avatar_sStr = [NSString stringWithUTF8String:(const char *)avatar_s];
            NSString *userNameStr = [NSString stringWithUTF8String:(const char *)userName];
            NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
            NSString *detailidStr = [NSString stringWithUTF8String:(const char *)detailid];
            
            TravelHomeModel *model = [[TravelHomeModel alloc]init];
           
            model.name = nameStr;
            model.popular_place_str = popular_place_strStr;
            model.cover_image = cover_imageStr;
            model.avatar_s = avatar_sStr;
            model.userName = userNameStr;
            model.title = titleStr;
            model.detailid = detailidStr;
            
            [array addObject:model];
            [model release];
        }
        return array;
    }
    else
    {
        NSLog(@"查询失败");
    }
    return nil;
}

- (void)dropTable
{
    NSString *dropSql = @"DROP TABLE travelHomePage";
    BOOL result = sqlite3_exec(db, dropSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除表成功");
    }
    else
    {
        NSLog(@"删除表失败");
    }
}

// 视图切换

//static sqlite3 *dbb = nil;
//
//
//- (void)openDBb
//{
//    if (dbb != nil) {
//        NSLog(@"数据库已经打开了");
//        return;
//    }
//    
//    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *documentPath = [array lastObject];
//    NSString *sqlitePath = [documentPath stringByAppendingPathComponent:@"travelHomePageb.sqlite"];
//    NSLog(@"%@", sqlitePath);
//    
//    BOOL result = sqlite3_open(sqlitePath.UTF8String, &dbb);
//    if (result == SQLITE_OK) {
//        NSLog(@"打开数据库");
//        
//    }
//    else
//    {
//        NSLog(@"打开数据库失败");
//    }
//}

- (void)creatDBb
{
    NSString *creatSql = @"CREATE TABLE IF NOT EXISTS travelHomePageb(address TEXT, photo_s TEXT, detailId TEXT)";
    BOOL result = sqlite3_exec(db, creatSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
        
    }
    else
    {
        NSLog(@"创建表失败");
    }
}

- (void)insertTravelphoto:(TravelNotesHomePageModel *)photoModel
{
    NSString *inserSql = [NSString stringWithFormat:@"INSERT INTO travelHomePageb(address, photo_s, detailId) VALUES('%@', '%@', '%@')", photoModel.address, photoModel.photo_s, photoModel.detailId];
    int result = sqlite3_exec(db, inserSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }
    else
    {
        NSLog(@"插入失败");
    }
    
}

- (NSMutableArray *)selectAllStudentb
{
    NSString *selectSql = @"SELECT * FROM travelHomePageb";
    sqlite3_stmt *stmt = nil;
    
    BOOL result = sqlite3_prepare_v2(db, selectSql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        NSMutableArray *array = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *address = sqlite3_column_text(stmt, 0);
            const unsigned char *photo_s = sqlite3_column_text(stmt, 1);
            const unsigned char *detailId = sqlite3_column_text(stmt, 2);
            
            
            NSString *addressStr = [NSString stringWithUTF8String:(const char *)address];
            NSString *photo_sStr = [NSString stringWithUTF8String:(const char *)photo_s];
            NSString *detailIdStr = [NSString stringWithUTF8String:(const char *)detailId];
            
            TravelNotesHomePageModel *model = [[TravelNotesHomePageModel alloc]init];
            model.address = addressStr;
            model.photo_s = photo_sStr;
            model.detailId = detailIdStr;
            
            
            [array addObject:model];
            [model release];
        }
        return array;
    }
    else
    {
        NSLog(@"查询失败");
    }
    return nil;
}

- (void)dropTableb
{
    NSString *dropSql = @"DROP TABLE travelHomePageb";
    BOOL result = sqlite3_exec(db, dropSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除表成功");
    }
    else
    {
        NSLog(@"删除表失败");
    }
}

+(CGFloat)getHeighContent:(NSString *)content
{
    CGSize size = CGSizeMake(MAIN.size.width - 20, 2000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    
    CGRect fram = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return fram.size.height;
    
}


@end
